package com.dq.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.commonutils.pojo.DqException;
import com.dq.commonutils.pojo.ResultCode;
import com.dq.eduservice.client.VodClient;
import com.dq.eduservice.entity.EduChapter;
import com.dq.eduservice.entity.EduVideo;
import com.dq.eduservice.service.EduChapterService;
import com.dq.eduservice.mapper.EduChapterMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dq.eduservice.service.EduVideoService;
import com.dq.eduservice.vo.chapter.ChapterVo;
import com.dq.eduservice.vo.chapter.VideoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程(EduChapter)表服务实现类
 *
 * @author Dq
 * @since 2022-07-22 19:50:35
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduChapterMapper chapterMapper;

    @Autowired
    private EduVideoService videoService;


    @Autowired
    private VodClient vodClient;

    @Override
    public List<ChapterVo> getChapterAndVideoByCourseId(String courseId) {
        /**
         * 1. 根据id当前的课程（course）查询所有的章节
         * 2. 然后根据所有的章节查询所有的小节
         * 3. 遍历各自list 最后进行封装
         * 4. 返回结果集对象
         */
        //1
        LambdaQueryWrapper<EduChapter> eduChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置条件
        eduChapterLambdaQueryWrapper.eq(EduChapter::getCourseId, courseId).orderByAsc(EduChapter::getGmtCreate);
        List<EduChapter> eduChapters = chapterMapper.selectList(eduChapterLambdaQueryWrapper);

        //2  直接使用mp
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getCourseId, courseId).orderByAsc(EduVideo::getGmtCreate);
        List<EduVideo> eduVideoList = videoService.list(eduVideoLambdaQueryWrapper);

        //3 封装数据
        List<ChapterVo> chapterVoList = new ArrayList<>();
        //3.1 遍历章节
        for (EduChapter eduChapter : eduChapters) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            //3.2 遍历小节 变成集合封装为List -->chapterVo 用于存储这个集合
            ArrayList<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideoList) {
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setVideoVoList(videoVoList);
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }

    @Override
    public void addChapter(EduChapter eduChapter) {
        /**
         * 直接添加章节
         */
        chapterMapper.insert(eduChapter);
    }

    @Override
    public ChapterVo getChapterById(String chapterId) {
        /**
         * 1. 查询chapter对象
         * 2. VO类封装
         */
        EduChapter eduChapter = chapterMapper.selectById(chapterId);
        ChapterVo chapterVo = new ChapterVo();
        BeanUtils.copyProperties(eduChapter,chapterVo);
        return chapterVo;
    }

    @Override
    public void updateChapterInfo(EduChapter eduChapter) {
        /**
         * 1. 根据id修改章节对象
         * 2. 判断是否修改成功
         * 3. 失败抛出异常
         */
        int update = chapterMapper.updateById(eduChapter);
        if (update == 0){
            throw new DqException(ResultCode.ERROR.getCode(), "章节修改失败");
        }
    }

    @Override
    public void deleteChapterInfoAndVideoById(String chapterId) {
        /**
         * 1. 获取对应的章节id
         * 2. 判断该章节下面是否有小节 如果没有则直接删除  有则进行下面操作
         * 3. 先根据对应的章节id删除小节所有信息
         * 4. 最后删除章节
         */
        if (StringUtils.isEmpty(chapterId)){
            throw new DqException(ResultCode.ERROR.getCode(), "异常错误");
        }
        List<EduVideo> eduVideos = videoService.getEduVideoByChapterId(chapterId);
        if (!eduVideos.isEmpty()) {
            List<String> videoArrayList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideos) {
                String videoSourceId = eduVideo.getVideoSourceId();
                if (!StringUtils.isEmpty(videoSourceId)){
                    videoArrayList.add(videoSourceId);
                }
            }
            //把小节视频id变为一个集合传入
            if (!videoArrayList.isEmpty()){
                vodClient.deleteAliyunVideoBatch(videoArrayList);
            }

            //删除该小节的所有信息
            videoService.deleteAllByChaperId(chapterId);

            //删除该章节
            int delete = chapterMapper.deleteById(chapterId);
            if (delete ==0){
                throw new DqException(ResultCode.ERROR.getCode(), "删除章节失败");
            }
        }else {
            int delete = chapterMapper.deleteById(chapterId);
            if (delete ==0){
                throw new DqException(ResultCode.ERROR.getCode(), "删除章节失败");
            }
        }
    }

    @Override
    public void deleteChapterByCourseId(String courseId) {
        /**
         * 1. 获取课程id
         * 2. 删除章节
         */
        LambdaQueryWrapper<EduChapter> eduChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduChapterLambdaQueryWrapper.eq(EduChapter::getCourseId,courseId);
        chapterMapper.delete(eduChapterLambdaQueryWrapper);
    }
}